﻿'Public Class SecurityGroups
'#Region " Events "
'    Public Shared Event Updated(ByVal group As Entities.SecurityGroup)
'    Public Shared Event UpdatedGUI(ByVal group As Entities.SecurityGroup)
'    Friend Shared Sub OnUpdated(ByVal group As Entities.SecurityGroup)
'        RaiseEvent Updated(group)
'        Framework.Threading.Invoke(AddressOf OnUpdated_Callback, group)
'    End Sub
'    Private Shared Sub OnUpdated_Callback(ByVal group As Entities.SecurityGroup)
'        RaiseEvent UpdatedGUI(group)
'    End Sub
'#End Region

'    Public Shared Sub SetupSystemGroups()
'        Dim clsSystemGroupAuthenticatedUsers As Entities.SecurityGroup
'        Using t As New TransactionScope
'            clsSystemGroupAuthenticatedUsers = GetByTypeAndSubType(Entities.SecurityGroupType.System, Entities.SecurityGroupSubType.AuthenticatedUsers)
'            If clsSystemGroupAuthenticatedUsers Is Nothing Then
'                clsSystemGroupAuthenticatedUsers = New Entities.SecurityGroup
'                clsSystemGroupAuthenticatedUsers.Name = "Authenticated Users"
'                clsSystemGroupAuthenticatedUsers.Description = "All buddies"
'                clsSystemGroupAuthenticatedUsers.Type = Entities.SecurityGroupType.System
'                clsSystemGroupAuthenticatedUsers.SubType = Entities.SecurityGroupSubType.AuthenticatedUsers
'                DataStore.SecurityGroups.Save(clsSystemGroupAuthenticatedUsers, t)

'                Dim aBuds As Generic.List(Of Entities.Buddy) = Framework.Buddies.Manager.GetAll
'                For Each clsBud As Entities.Buddy In aBuds
'                    DataStore.SecurityGroups.AddGroupMember(clsSystemGroupAuthenticatedUsers.Id, clsBud.Id, t)
'                Next
'            End If
'            t.Complete()
'        End Using

'        Dim aShares As Generic.List(Of Entities.Share) = Shares.GetAll
'        For Each clsShare As Entities.Share In aShares
'            If clsShare.SecurityGroupIds.Count = 0 Then
'                clsShare.SecurityGroupIds.Add(clsSystemGroupAuthenticatedUsers.Id)
'                Shares.Save(clsShare)
'            End If
'        Next

'    End Sub

'    Public Shared Sub Save(ByVal group As Entities.SecurityGroup, ByVal transactionScope As TransactionScope)
'        DataStore.SecurityGroups.Save(group, transactionScope)
'        transactionScope.RegisterOnCompleteCallback(AddressOf OnUpdated_TransactionCallback, "SecurityGroupsUpdate", group.Id, group)
'    End Sub
'    Private Shared Sub OnUpdated_TransactionCallback(ByVal arg1 As String, ByVal arg2 As Integer, ByVal arg3 As Object)
'        OnUpdated(CType(arg3, Entities.SecurityGroup))
'    End Sub
'    Public Shared Function GetByTypeAndSubType(ByVal type As Entities.SecurityGroupType, ByVal subType As Entities.SecurityGroupSubType) As Entities.SecurityGroup
'        Return DataStore.SecurityGroups.GetByTypeAndSubType(type, subType)
'    End Function
'    Public Shared Function GetAllByBuddyId(ByVal buddyId As Integer) As Generic.List(Of Entities.SecurityGroup)
'        Return DataStore.SecurityGroups.GetAllByBuddyId(buddyId)
'    End Function
'    Public Shared Function GetAll() As Generic.List(Of Entities.SecurityGroup)
'        Return DataStore.SecurityGroups.GetAll
'    End Function
'    Public Shared Function GetAllMembers(ByVal groupId As Integer) As Generic.List(Of Entities.Buddy)
'        Dim aIds As Generic.List(Of Integer)
'        aIds = DataStore.SecurityGroups.GetAllMemberIds(groupId)
'        Return Framework.Buddies.Manager.GetAllByIds(aIds)
'    End Function

'    Public Shared Sub SetBuddyMembersList(ByVal buddyId As Integer, ByVal securityGroupIds As Generic.List(Of Integer), ByVal transactionScope As TransactionScope)
'        DataStore.SecurityGroups.RemoveAllGroupMembershipsForBuddy(buddyId, transactionScope)
'        For Each iId As Integer In securityGroupIds
'            DataStore.SecurityGroups.AddGroupMember(iId, buddyId, transactionScope)
'        Next
'    End Sub
'    Public Shared Sub AddBuddyToGroup(ByVal buddyId As Integer, ByVal securityGroupId As Integer, ByVal transactionScope As TransactionScope)
'        DataStore.SecurityGroups.AddGroupMember(securityGroupId, buddyId, transactionScope)
'    End Sub
'    Public Shared Sub SetGroupMembers(ByVal securityGroupId As Integer, ByVal buddyIds As Generic.List(Of Integer), ByVal transactionScope As TransactionScope)
'        DataStore.SecurityGroups.RemoveAllMembers(securityGroupId, transactionScope)
'        For Each iId As Integer In buddyIds
'            DataStore.SecurityGroups.AddGroupMember(securityGroupId, iId, transactionScope)
'        Next
'    End Sub
'End Class

'Public Class SecurityKeyMaster
'    Private Shared mAnonymous As New SecurityKeyMaster(-1)
'    Public Shared Function Anonymous() As SecurityKeyMaster
'        Return mAnonymous
'    End Function
'    Public Shared Function GetByBuddyId(ByVal buddyId As Integer) As SecurityKeyMaster
'        Dim clsB As Entities.Buddy = Framework.Buddies.Manager.GetById(buddyId)
'        If clsB Is Nothing Then
'            Return mAnonymous
'        Else
'            Dim clsRet As New SecurityKeyMaster(buddyId)
'            clsRet.SetGroupMemberShips(SecurityGroups.GetAllByBuddyId(buddyId))
'            Return clsRet
'        End If
'    End Function

'    Public ReadOnly BuddyId As Integer
'    Friend Sub New(ByVal buddyId As Integer)
'        Me.BuddyId = buddyId
'        AddHandler Framework.Buddies.Manager.Updated, AddressOf EventHandler_BuddyUpdated
'    End Sub
'    Private Sub EventHandler_BuddyUpdated(ByVal buddy As Entities.Buddy)
'        Try
'            If BuddyId = buddy.Id Then SetGroupMemberShips(SecurityGroups.GetAllByBuddyId(buddy.Id))
'        Catch ex As Exception
'            mGroups.Clear()
'            Framework.Misc.HandleException(ex)
'        End Try
'    End Sub

'    Private mGroups As New Generic.HashSet(Of Integer)
'    Private Sub SetGroupMemberShips(ByVal groups As Generic.List(Of Entities.SecurityGroup))
'        Dim aTmp As New HashSet(Of Integer)
'        For Each clsG As Entities.SecurityGroup In groups
'            aTmp.Add(clsG.Id)
'        Next
'        mGroups = aTmp
'    End Sub


'    Public Function IsGranted(ByVal share As Entities.Share) As Boolean
'        If share Is Nothing Then Return False
'        For Each iId As Integer In share.SecurityGroupIds
'            If mGroups.Contains(iId) Then Return True
'        Next
'        Return False
'    End Function
'    Public Function IsGranted(ByVal item As Entities.SharedItem) As Boolean
'        If item Is Nothing Then Return False
'        Dim clsShare As Entities.Share = Shares.GetById(item.ShareId)
'        If clsShare Is Nothing Then
'            Return False
'        Else
'            Return IsGranted(clsShare)
'        End If
'    End Function
'    Public Function IsGranted(ByVal file As Entities.SharedItemFile) As Boolean
'        If file Is Nothing Then Return False

'        Dim clsItem As Entities.SharedItem = SharedItems.GetById(file.SharedItemId)
'        If clsItem Is Nothing Then Return False

'        Dim clsShare As Entities.Share = Shares.GetById(clsItem.ShareId)
'        If clsShare Is Nothing Then
'            Return False
'        Else
'            Return IsGranted(clsShare)
'        End If
'    End Function
'    Public Function IsGranted(ByVal securityGroupIds As HashSet(Of Integer)) As Boolean
'        For Each iId As Integer In securityGroupIds
'            If mGroups.Contains(iId) Then Return True
'        Next
'        Return False
'    End Function


'End Class
